<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item
              label="商品名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.goodsName"
                placeholder="请输入商品名称"
                @change="handleNameChange"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="商品编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.goodsCode"
                placeholder="请输入商品编号"
                @change="handleNameChange"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <a-card class="card-content">
      <a-row :gutter="24">
        <a-col :span="22">
          <a-button type="primary" @click="handleAdd">新建BOM</a-button>

          <!-- <a-button  type="primary">导出</a-button> -->
        </a-col>
        <a-col :span="2">
          <a-button type="primary" @click="handleAllList('ProductList')"
            >全部产品</a-button
          >
        </a-col>
      </a-row>
      <br />
      <a-table
        :columns="columns"
        :data-source="tableData"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
        bordered
        :rowKey="(record, index) => record.id"
      >
        <template slot="action" slot-scope="text, record">
          <a
            v-hasPromise="'productDetail'"
            @click="handleToDetail('/product/detail', record)"
            >编辑</a
          >
          <a-divider v-hasPromise="'productDetail'" type="vertical" />
          <a @click="handleEdit(record)">复制</a>
          <!-- <a-divider type="vertical" />
          <a-popconfirm
           okText="确定"
            cancelText="取消"
            v-if="tableData.length"
            title="确定删除吗?"
            @confirm="() => handleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm> -->
        </template>
      </a-table>
      <ProductModal
        ref="modalForm"
        @ok="modalFormOk"
        :treeData="treeData"
      ></ProductModal>
    </a-card>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import ProductModal from "./components/ProductModal.vue";
export default {
  components: { ProductModal },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      treeData: [],
      tableData: [],
      goodsOption: [], // 商品选择数据源
    };
  },
  activated() {
    let queryParam = localStorage.getItem("queryParam");
    if (queryParam) {
      this.getList();
      this.getTree();
    } else {
      this.queryParam = {};
      this.getList();
      this.getTree();
    }
  },
  mounted() {
    this.getList();
    this.getTree();
  },
  methods: {
    // 商品查询
    handleGoodSearch(goodsName) {
      this.getGoodList(goodsName);
    },
    handleNameChange(value) {
      if (value) {
        localStorage.setItem("queryParam", JSON.stringify(this.queryParam));
      }
    },
    // 获取商品列表
    async getGoodList(goodsName) {
      let params = { pageNumber: 1, pageSize: 10 };
      if (goodsName) {
        params.goodsName = goodsName;
        const res = await api.CommodityList(params);
        this.goodsOption = res.data.data;
      }
    },
    // 获取列表数据
    async getList(arg) {
      this.loading = true;
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.ProductList(params);
      this.tableData = res.data.data;
      this.pagination.total = res.data.total;
      this.loading = false;
    },
    handleEdit(record) {
      this.$refs.modalForm.title = "复制";
      this.$refs.modalForm.modalEdit(record);
    },
    handleToDetail(path, record) {
      localStorage.setItem("queryParam", JSON.stringify(this.queryParam));
      let { id, bomNum, bomName } = record;
      let query = { id, bomNum, bomName };
      this.$router.push({ path, query });
    },
    // 删除
    async handleDelete(record) {
      let params = {
        id: record.id,
      };
      const res = await api.ProductDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
    async getTree() {
      const res = await api.CommodityTypeFindAll();
      this.treeData = res.data;
      console.log(this.treeData);
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
